import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_swiper/flutter_swiper.dart';
import 'SingleTreeHolePage.dart';
import 'MyTreeHolePage.dart';
import 'TreeHoleList.dart';
import 'BuildTreeHolePage.dart';

//树洞(随机index、缩略滑动、图片加载延迟待解决)
class TreeHolePage extends StatefulWidget {
  final String token;
  TreeHolePage(this.token);
  @override
  _TreeHolePage createState() => new _TreeHolePage();
}

class _TreeHolePage extends State<TreeHolePage> {
  Widget build(BuildContext context){
    return Scaffold(
      appBar: AppBar(
        title: Container(
          child: Text('树洞',
            style: TextStyle(
                color: Colors.black,
                letterSpacing: 5
            ),
          ),
        ),
        centerTitle:true,
        backgroundColor: Colors.white,
        leading: IconButton(
          icon:Icon(Icons.arrow_back,color: Colors.black),
          onPressed: () {Navigator.pop(context);},
        ),
      ),
      body: Container(
        width: double.infinity,
        height: double.infinity,
        padding: EdgeInsets.all(20),
        child: Column(
          //mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Expanded(
              flex: 5,
              child: MySwiper(widget.token),),
            Expanded(
              flex: 1,
                child: Align(
                alignment: Alignment.center,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [_BuildTreeHoleButton(widget.token),SizedBox(width: 15,),_MyTreeHoleButton(widget.token)],
                )
            ))

          ],
        ),
        decoration: BoxDecoration(
            image: new DecorationImage(
                fit: BoxFit.cover,
                image: new AssetImage('images/treeHole.jpg')
            )
        ),
      ),
    );
  }
}
//创建树洞按钮
class _BuildTreeHoleButton extends StatelessWidget{
  final String token;
  _BuildTreeHoleButton(this.token);
  Widget build (BuildContext context){
    return new Padding(
      padding: EdgeInsets.only(bottom: 20),
      child: RaisedButton(
        shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(10)
        ),
        child: Text('创建树洞'),
        textColor: Colors.black,
        color: Colors.green[100],
        padding: EdgeInsets.all(10),
        onPressed: () {print(this.token);Navigator.push(context, new MaterialPageRoute(builder: (context) => BuildTreeHolePage(this.token)));},
      ),
    );
  }
}
//我的树洞按钮
class _MyTreeHoleButton extends StatelessWidget{
  final String token;
  _MyTreeHoleButton(this.token);
  Widget build (BuildContext context){
    return new Padding(
      padding: EdgeInsets.only(bottom: 20),
      child: RaisedButton(
        shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(10)
        ),
        child: Text('我的树洞'),
        textColor: Colors.black,
        color: Colors.green[100],
        padding: EdgeInsets.all(10),
        onPressed: () {Navigator.push(context, new MaterialPageRoute(builder: (context) => MyTreeHolePage(this.token)));},
      ),
    );
  }
}

//轮播图
class MySwiper extends StatefulWidget {
  String token;
  MySwiper(this.token,{Key key}) : super(key: key);
  @override
  _MySwiper createState() => new _MySwiper();
}
class _MySwiper extends State<MySwiper> {
  List articleList;
  @override
  void initState() {
    super.initState();
    TreeHoleList.getAllArticles().then((result) {
      setState(() {
        articleList = result;
      });
    }).whenComplete(() {
      print("getAllArticles异步任务处理完成");
    });
  }

  @override
  Widget build(BuildContext context) {
      return SingleChildScrollView(
        child: Container(
          padding: EdgeInsets.all(10),
            child: new AspectRatio(
                aspectRatio: 28/35,
              child: Swiper(
                itemBuilder: (BuildContext context, int index) {
                  return Container(
                    //padding: EdgeInsets.all(15),
                    color: Colors.white54,
                    child: ListView(
                      children: [
                        ListTile(
                          title: Text(articleList[index]['title']!=null?articleList[index]['title']:'匿名树洞',
                            textAlign: TextAlign.center,
                          ),
                          subtitle: Text(articleList[index]['content'],
                            style: TextStyle(
                                fontSize: 15
                            ),
                          ),
                        ),
                        Row(children: [
                          Spacer(),
                          Padding(padding: EdgeInsets.only(right: 10,bottom: 10),child: Text(articleList[index]['createTime']),)]),
                      ],
                    ),
                  );
                },
                itemCount: articleList!=null?articleList.length:0,
                // control: new SwiperControl(
                //     color: Colors.black
                // ),
                onTap: (index) async {
                  var _article= await TreeHoleList.getArticle(articleList[index]['id']);
                  Navigator.push(context, new MaterialPageRoute(
                      builder: (context) => SingleTreeHolePage(token:widget.token,article:_article)));
                },
                loop: false,
              ),
            )
        ),
      );
  }
}